home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / text / tex / TeXFormat.lha / TEXFORMAT / MUI / TeXFormat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-03  |  33.8 KB  |  779 lines

  1. /***************************************************************************
  2.  *                                                                         *
  3.  *  Filename : TeXFormat.c                                                 *
  4.  *                                                                         *
  5.  *  Description : A front-end for the PasTeX TeX implementation.           *
  6.  *                The program looks for the format files found in the path *
  7.  *                TeX:Formats and enables the user to switch between the   *
  8.  *                available format files via a gadtools interface.         *
  9.  *                                                                         *
  10.  *  Return Codes :   0 - Ordinary program halt.                            *
  11.  *                  10 - No format files available.                        *
  12.  *                  20 - Couldn't open MUI application.                    *
  13.  *                  30 - No mui.library.                                   *
  14.  *                                                                         *
  15.  ***************************************************************************
  16.  *                                                                         *
  17.  *                         Modification History                            *
  18.  *                                                                         *
  19.  *  Date      Author       Comments                                        *
  20.  * ----------------------------------------------------------------------  *
  21.  *  9.3.92    R.Bödi       Created.                                        *
  22.  *  8.6.92    R.Bödi       Fixed bug (first format entry couldn't be       *
  23.  *                         selected.)                                      *
  24.  *  5.9.93    R.Bödi       Now TeXFormat is a MUI application.             *
  25.  * 14.9.93    R.Bödi       Added 'Save' button.                            *
  26.  *                                                                         *
  27.  ***************************************************************************
  28.  *                                                                         *
  29.  * Copyright © 1992-1993 Richard Bödi,  All rights reserved.               *
  30.  *                                                                         *
  31.  ***************************************************************************/
  32.  
  33. /*----------------------------- INCLUDES ----------------------------------*/
  34.  
  35. #include "string.h"
  36. #include "stdio.h"
  37. #include "stdlib.h"
  38. #include "dos.h"
  39.  
  40. #include "workbench/workbench.h"
  41. #include "workbench/startup.h"
  42. #include "libraries/mui.h"
  43.  
  44. #include "clib/alib_protos.h"
  45. #include "clib/intuition_protos.h"
  46. #include "clib/exec_protos.h"
  47. #include "clib/dos_protos.h"
  48. #include "clib/graphics_protos.h"
  49. #include "clib/gadtools_protos.h"
  50. #include "clib/icon_protos.h"
  51. #include "clib/wb_protos.h"
  52. #include "clib/muimaster_protos.h"
  53.  
  54. #include "pragmas/intuition_pragmas.h"
  55. #include "pragmas/exec_pragmas.h"
  56. #include "pragmas/dos_pragmas.h"
  57. #include "pragmas/graphics_pragmas.h"
  58. #include "pragmas/gadtools_pragmas.h"
  59. #include "pragmas/icon_pragmas.h"
  60. #include "pragmas/wb_pragmas.h"
  61. #include "pragmas/muimaster_pragmas.h"
  62.  
  63.  
  64. /*--------------------------- DEFINES -------------------------------------*/
  65.  
  66. #define  VERSION        " V2.00 "
  67.  
  68. #define  COMMENTLENGTH         82
  69. #define  CMDSTRLENGTH         512
  70.  
  71. #define  ID_FORMAT_SEL          1
  72. #define  ID_SAVE                2
  73. #define  ID_ABOUT            1000
  74. #define  ID_HIDE            10000
  75. #define  ID_SHOW            10001
  76.  
  77. #define  NEWLINE             "\n"
  78.  
  79. /*-------------------- STRUCTURE DEFINITIONS ------------------------------*/
  80.  
  81. enum ScanError  { SCAN_OK, NO_LOCK, NO_FORMAT, NO_ENV, NO_MATCH, NO_MEMORY };
  82. enum ErrorIDs   { ERR_NOERROR, ERR_FORMATFILES = 10, ERR_MUI = 20, ERR_NOMUILIB = 30 };
  83. enum Keywords   { KW_CURRENTFORMAT, KW_FORMATPATH, KW_PATTERN, LAST_KW };
  84.  
  85.  
  86. struct FormatNode
  87.    {
  88.    struct FormatNode   *NextNode;
  89.    char                 Name[FNSIZE];
  90.    char                 Comment[COMMENTLENGTH];
  91.    int                  ID;
  92.    };
  93.  
  94. struct TeXFormat
  95.    {
  96.    struct FormatNode   *FirstNode;
  97.    char               **FormatArray;
  98.    struct FormatNode   *CurrentFormat;
  99.    int                  MaxCommentLength;
  100.    int                  MaxCommentLines;
  101.    char                 DummyComment[COMMENTLENGTH];
  102.    int                  NoOfFormats;
  103.    };
  104.  
  105. struct Settings
  106.    {
  107.    long     ArgPtr[LAST_KW];
  108.    char     ArgStr[LAST_KW][FMSIZE];              // FMSIZE see file dos.h
  109.    char     CurrentFormat[FNSIZE];
  110.    };
  111.  
  112.  
  113. /*--------------------- FUNCTION PROTOTYPES -------------------------------*/
  114.  
  115. BOOL              OpenLibs (void);
  116. void              ParseShell (int, char **, struct Settings *);
  117. void              ParseWB (struct WBStartup *, struct Settings *);
  118. enum ScanError    ScanFormatPath (struct TeXFormat *, struct Settings *);
  119. void              HandleIDCMP (struct TeXFormat *, struct Settings *);
  120. int               LineLength (char *);
  121. int               NoOfLines (char *);
  122. char             *ConvertNL (char *);
  123. void              Cleanup (char *, enum ErrorIDs);
  124.  
  125.  
  126. /*------------------------ EXTERNAL VARIABLES ----------------------------*/
  127.  
  128. extern struct Library         *SysBase;
  129. extern struct DOSBase         *DOSBase;
  130. extern struct GfxBase         *GfxBase;
  131. extern struct IntuitionBase   *IntuitionBase;
  132. extern struct GadToolsBase    *GadToolsBase;
  133. extern struct IconBase        *IconBase;
  134. extern struct WorkbenchBase   *WorkbenchBase;
  135.  
  136. /*-------------------------- GLOBAL DATA ----------------------------------*/
  137.  
  138. struct Library                *MUIMasterBase;
  139.  
  140. char  Version[]   = "$VER: TeXFormat  Release " VERSION " (" __DATE__ ")         © R.Bödi";
  141. char  Title[]     = "TeXFormat " VERSION "   © R.Bödi";
  142. char  Author[]    = "Richard Bödi\nKäsenbachstr. 10/2\n72076 Tübingen\nGermany\n\ne-mail: mmisa01@mailserv.zdv.uni-tuebingen.de";
  143.  
  144. char  *Keywords[] = { "CURRENTFORMAT", "FORMATPATH", "PATTERN" };
  145. char  *Switchs[]  = { "/K", "/K", "/K" };
  146.  
  147. STRPTR  ErrorMsg[] = { "",
  148.                        "Couldn't find directory",
  149.                        "Couldn't find a TeX format file in ",
  150.                        "Environment variable unset",
  151.                        "Couldn't find TeX format file defined by the environment variable ",
  152.                        "Not enough memory available" };
  153.  
  154. struct NewMenu Menu[] = { NM_TITLE, "File", NULL, 0, 0, NULL,
  155.                           NM_ITEM, "About",   NULL, 0, 0, (APTR)ID_ABOUT,
  156.                           NM_ITEM, NM_BARLABEL,  NULL, 0, 0, NULL,
  157.                           NM_ITEM, "Quit",    "Q",  0, 0, (APTR)MUIV_Application_ReturnID_Quit,
  158.                           NM_END };
  159.  
  160. /*--------------------------- MUI Objects ---------------------------------*/
  161.  
  162. Object   *App;
  163. Object   *Win;
  164. Object   *Select_Gad;
  165. Object   *Save_Gad;
  166. Object   *Done_Gad;
  167. Object   *Description_TextField;
  168.  
  169. /*------------------------------- CODE ------------------------------------*/
  170.  
  171. main (int argc, char *argv[])
  172.  
  173. {
  174. struct TeXFormat  TeXFormat;
  175. struct Settings   Settings;
  176. enum ScanError    FormatAvail;
  177. char              ErrorStrg[FMSIZE];
  178.  
  179.  
  180. if (argc == 0) ParseWB ((struct WBStartup *)argv, &Settings);
  181. if (argc > 1)  ParseShell (argc, argv, &Settings);
  182.  
  183. if (OpenLibs ())
  184.    {
  185.    FormatAvail = ScanFormatPath (&TeXFormat, &Settings);
  186.  
  187.    if (FormatAvail == SCAN_OK)
  188.       {
  189.       App = ApplicationObject,
  190.                  MUIA_Application_Title      , "TeXFormat",
  191.                  MUIA_Application_Version    , Version,
  192.                  MUIA_Application_Copyright  , "© 1992-93, Richard Bödi",
  193.                  MUIA_Application_Author     , Author,
  194.                  MUIA_Application_Description, "Select TeX format files easily.",
  195.                  MUIA_Application_Base       , "TEXFORMAT",
  196.                  MUIA_Application_Menu       , Menu,
  197.                  SubWindow, Win = WindowObject,
  198.                      MUIA_Window_ID, 0,
  199.                      MUIA_Window_Title, Title,
  200.                      WindowContents, VGroup,
  201.                          Child, HGroup,
  202.                                Child, Select_Gad = Radio ("TeX format", TeXFormat.FormatArray),
  203.                                End,
  204.                      Child, HGroup, GroupFrameT ("Description"),
  205.                                Child, Description_TextField = TextObject, TextFrame,
  206.                            MUIA_Background, MUII_TextBack,
  207.                            MUIA_Text_Contents, TeXFormat.DummyComment,
  208.                            End,
  209.                         End,
  210.                            Child, VSpace(0),
  211.                      Child, HGroup,
  212.                         Child, Save_Gad = KeyButton ("Save", 's'),
  213.                         Child, HSpace(0),
  214.                         Child, HSpace(0),
  215.                         Child, Done_Gad = KeyButton ("Done", 'd'),
  216.                         End,
  217.                      End,
  218.                   End,
  219.                End;
  220.  
  221.       if (App)
  222.          {
  223.          DoMethod (Win, MUIM_Notify,
  224.                    MUIA_Window_CloseRequest, TRUE, App, 2,
  225.                    MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
  226.  
  227.          DoMethod (App, MUIM_Notify,
  228.                    MUIA_Application_Iconified, FALSE, App, 2,
  229.                    MUIM_Application_ReturnID, ID_SHOW);
  230.  
  231.          DoMethod (App, MUIM_Notify,
  232.                    MUIA_Application_Iconified, TRUE, App, 2,
  233.                    MUIM_Application_ReturnID, ID_HIDE);
  234.  
  235.          DoMethod (Select_Gad, MUIM_Notify,
  236.                    MUIA_Radio_Active, MUIV_EveryTime, App, 2,
  237.                    MUIM_Application_ReturnID, ID_FORMAT_SEL);
  238.  
  239.          DoMethod (Save_Gad, MUIM_Notify,
  240.                    MUIA_Pressed, FALSE, App, 2,
  241.                    MUIM_Application_ReturnID, ID_SAVE);
  242.  
  243.          DoMethod (Done_Gad, MUIM_Notify,
  244.                    MUIA_Pressed, FALSE, App, 2,
  245.                    MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
  246.  
  247.          DoMethod (Win, MUIM_Window_SetCycleChain, Select_Gad, Save_Gad, Done_Gad, NULL);
  248.  
  249.          set (Win, MUIA_Window_Open, TRUE);
  250.          set (Select_Gad, MUIA_Radio_Active, TeXFormat.CurrentFormat->ID);
  251.  
  252.          HandleIDCMP (&TeXFormat, &Settings);
  253.  
  254.          Cleanup (NULL, ERR_NOERROR);
  255.          }
  256.       else
  257.          Cleanup ("Couldn't create MUI application.", ERR_MUI);
  258.       }
  259.    else
  260.       {
  261.       sprintf (ErrorStrg, "%s%s\n", ErrorMsg[FormatAvail], Settings.ArgStr[KW_FORMATPATH]);
  262.       Cleanup (ErrorStrg, ERR_FORMATFILES);
  263.       }
  264.    }
  265. else
  266.    {
  267.    sprintf (ErrorStrg, "No muimaster.library of version >= %d\n", MUIMASTER_VMIN);
  268.    Cleanup (ErrorStrg, ERR_NOMUILIB);
  269.    }
  270. }
  271.  
  272.  
  273. /***************************************************************************
  274.  *                                                                         *
  275.  *  Function name : ParseWB                                                *
  276.  *                                                                         *
  277.  *  Description : Fetches and parses the ToolTypes from the TeXFormat icon.*
  278.  *                Allowed tooltypes are :                                  *
  279.  *                   CURRENTFORMAT (Specifies the name of the environment  *
  280.  *                                  variable in which the name of the      *
  281.  *                                  current TeX format file is stored.)    *
  282.  *                   FORMATPATH (Specifies the path for the TeX            *
  283.  *                               format files is stored.)                  *
  284.  *                   PATTERN (Specifies the file pattern for fromat files. *
  285.  *                            Typically it's #?.fmt.)                      *
  286.  *                                                                         *
  287.  ***************************************************************************
  288.  *                                                                         *
  289.  *  Synopsis : ParseWB (WBArgStr, Settings)                                *
  290.  *                                                                         *
  291.  *  Parameters :                                                           *
  292.  *    (struct WBStartup **) WBArgStr:                                      *
  293.  *          The WB argument string.                                        *
  294.  *    (struct Settings *) Settings:                                        *
  295.  *          The tags of theses structure are filled by this procedure.     *
  296.  *                                                                         *
  297.  *  Return value : None                                                    *
  298.  *                                                                         *
  299.  ***************************************************************************/
  300.  
  301. void  ParseWB (struct WBStartup *WBArgStr, struct Settings *Settings)
  302.  
  303. {
  304. struct DiskObject   *DiskObj;
  305. struct WBArg        *WBArg;
  306. char               **ToolArray;
  307.  
  308.  
  309. WBArg = WBArgStr->sm_ArgList;
  310.  
  311. if (WBArg->wa_Name)     // get tooltypes
  312.    if (DiskObj = GetDiskObject (WBArg->wa_Name))
  313.       {
  314.       ToolArray = (char **)DiskObj->do_ToolTypes;
  315.       stpcpy (Settings->ArgStr[KW_CURRENTFORMAT], FindToolType (ToolArray, Keywords[KW_CURRENTFORMAT]));
  316.       stpcpy (Settings->ArgStr[KW_FORMATPATH], FindToolType (ToolArray, Keywords[KW_FORMATPATH]));
  317.       stpcpy (Settings->ArgStr[KW_PATTERN], FindToolType (ToolArray, Keywords[KW_PATTERN]));
  318.       FreeDiskObject (DiskObj);
  319.       }
  320.  
  321. GetVar (Settings->ArgStr[KW_CURRENTFORMAT], Settings->CurrentFormat, sizeof (Settings->CurrentFormat), GVF_GLOBAL_ONLY);
  322. }
  323.  
  324.  
  325.  
  326. /***************************************************************************
  327.  *                                                                         *
  328.  *  Function name : ParseShell                                             *
  329.  *                                                                         *
  330.  *  Description : Parses the commandline for parameters.                   *
  331.  *                Allowed parameters are :                                 *
  332.  *                   CURRENTFORMAT/K (Specifies the name of the environment*
  333.  *                                    variable in which the name of the    *
  334.  *                                    current TeX format file is stored.   *
  335.  *                   FORMATPATH/K (Specifies the the path for the TeX      *
  336.  *                                 format files is stored.                 *
  337.  *                   PATTERN(K (Specifies the file pattern for fromat files*
  338.  *                              Typically it's #?.fmt.)                    *
  339.  *                                                                         *
  340.  ***************************************************************************
  341.  *                                                                         *
  342.  *  Synopsis : ParseShell (NoOfArgs, Arg, Settings)                        *
  343.  *                                                                         *
  344.  *  Parameters :                                                           *
  345.  *    (int) NoOfArgs:                                                      *
  346.  *          The number of arguments.                                       *
  347.  *    (char **) Arg:                                                       *
  348.  *          The shell argument string.                                     *
  349.  *    (struct Settings *) Settings:                                        *
  350.  *          The tags of theses structure are filled by this procedure.     *
  351.  *                                                                         *
  352.  *  Return value : None                                                    *
  353.  *                                                                         *
  354.  ***************************************************************************/
  355.  
  356. void  ParseShell (int NoOfArgs, char **Arg, struct Settings *Settings)
  357.  
  358. {
  359. int            kword;
  360. struct RDArgs *RDArgs;
  361. char           Template[CMDSTRLENGTH];
  362.  
  363.  
  364. for (kword = 0, Template[0] = 0; kword < LAST_KW; kword++)
  365.    {
  366.    if (Template[0]) strcat (Template, ",");
  367.    strcat (Template, Keywords[kword]);
  368.    strcat (Template, Switchs[kword]);
  369.    }
  370.  
  371. if (RDArgs = ReadArgs (Template, Settings->ArgPtr, NULL))
  372.    {
  373.    for (kword = 0; kword < LAST_KW; kword++)
  374.       if (Settings->ArgPtr[kword])
  375.          stpcpy (Settings->ArgStr[kword], (char *)Settings->ArgPtr[kword]);
  376.    }
  377. else
  378.    PrintFault (IoErr (), "Error reading arguments");
  379.  
  380. GetVar (Settings->ArgStr[KW_CURRENTFORMAT], Settings->CurrentFormat, sizeof (Settings->CurrentFormat), GVF_GLOBAL_ONLY);
  381.  
  382. FreeArgs (RDArgs);
  383. }
  384.  
  385.  
  386. /***************************************************************************
  387.  *                                                                         *
  388.  *  Function name : ScanFormatPath                                         *
  389.  *                                                                         *
  390.  *  Description : Scans the format path for TeX format files, creates      *
  391.  *                and fills for each file a struct FormatNode, updates     *
  392.  *                the TeXFormat structure and reads the Settings->Texformat*
  393.  *                environment variable.                                    *
  394.  *                                                                         *
  395.  ***************************************************************************
  396.  *                                                                         *
  397.  *  Synopsis : ScanFormatPath (TeXFormat, Settings)                        *
  398.  *                                                                         *
  399.  *  Parameters :                                                           *
  400.  *    (struct TeXFormat *) TeXFormat:                                      *
  401.  *          This structure will be filled with the names of the format     *
  402.  *          files found.                                                   *
  403.  *    (struct Settings *) Settings:                                        *
  404.  *          Contains the path and extension of the format files and the    *
  405.  *          name of the TEXFORMAT enviroenment variable.                   *
  406.  *                                                                         *
  407.  *  Return value : (enum ScanError)                                        *
  408.  *    SCAN_OK, if at least one TeX format file was found and the content   *
  409.  *             this variable matches a scanned TeX format file.            *
  410.  *    NO_LOCK, if path couldn't be locked for examination.                 *
  411.  *  NO_FORMAT, if no TeX format file was found.                            *
  412.  *     NO_ENV, if one of the environment variables is unset.               *
  413.  *   NO_MATCH, if content of the CURRENTFORMAT environment variable        *
  414.  *             matches no scanned format file.                             *
  415.  *  NO_MEMORY, if no memory for the FormatNodes was available.             *
  416.  *                                                                         *
  417.  ***************************************************************************/
  418.  
  419. enum ScanError    ScanFormatPath (struct TeXFormat *TeXFormat, struct Settings *Settings)
  420.  
  421. {
  422. int                  Index;
  423. BPTR                 OldLock, NewLock;
  424. BOOL                 ActFormatFound = FALSE, Match, Error = FALSE;
  425. struct AnchorPath    APath;
  426. struct FormatNode   *NewNode, *LastNode = NULL;
  427. char                 FormatName[FNSIZE];
  428.  
  429.  
  430. APath.ap_BreakBits          = 0;
  431. APath.ap_Strlen             = 0;
  432. TeXFormat->FirstNode        = NULL;
  433. TeXFormat->MaxCommentLength = 1;
  434. TeXFormat->MaxCommentLines  = 1;
  435.  
  436. if ((strlen (Settings->CurrentFormat) != 0) && (strlen (Settings->ArgStr[KW_FORMATPATH]) != 0))
  437.    {
  438.    if (NewLock = Lock (Settings->ArgStr[KW_FORMATPATH], ACCESS_READ))
  439.       {
  440.       OldLock = CurrentDir (NewLock);
  441.  
  442.       for (Index = 0, Match = MatchFirst (Settings->ArgStr[KW_PATTERN], &APath); !Match; Match = MatchNext (&APath), Index++)
  443.          {
  444.          SplitName (APath.ap_Info.fib_FileName, '.', FormatName, 0, sizeof (FormatName));
  445.          if (strlen (FormatName) == 0) Index--;
  446.          else
  447.             {
  448.             if (NewNode = (struct FormatNode *) calloc (sizeof (struct FormatNode), 1))
  449.                {
  450.                if (TeXFormat->FirstNode == NULL) TeXFormat->FirstNode = NewNode;
  451.                else                              LastNode->NextNode   = NewNode;
  452.  
  453.                sprintf (NewNode->Name, "%-s", FormatName);
  454.                stpcpy (NewNode->Comment, APath.ap_Info.fib_Comment);
  455.                LastNode = NewNode;
  456.  
  457.                if (!strcmpi (Settings->CurrentFormat, FormatName))
  458.                   {
  459.                   TeXFormat->CurrentFormat  = NewNode;
  460.                   ActFormatFound            = TRUE;
  461.                   }
  462.                if (LineLength (NewNode->Comment) > TeXFormat->MaxCommentLength)
  463.                   TeXFormat->MaxCommentLength = LineLength (NewNode->Comment);
  464.                if (NoOfLines (NewNode->Comment) > TeXFormat->MaxCommentLines)
  465.                   TeXFormat->MaxCommentLines = NoOfLines (NewNode->Comment);
  466.                if (NoOfLines (NewNode->Comment) > 1)
  467.                   ConvertNL (NewNode->Comment);
  468.                }
  469.             else
  470.                {
  471.                Error = TRUE;
  472.                break;
  473.                }
  474.             }
  475.          }
  476.  
  477.       MatchEnd (&APath);
  478.       CurrentDir (OldLock);
  479.       UnLock (NewLock);
  480.  
  481.       TeXFormat->NoOfFormats  = Index;
  482.  
  483.       if (Error)                    return (NO_MEMORY);
  484.       if (!TeXFormat->NoOfFormats)  return (NO_FORMAT);
  485.       if (!ActFormatFound)          return (NO_MATCH);
  486.  
  487.       TeXFormat->FormatArray  = (char **)calloc (TeXFormat->NoOfFormats + 1, sizeof (TeXFormat->FormatArray[0]));
  488.  
  489.       for (Index = 0, TeXFormat->DummyComment[0] = 0; Index < TeXFormat->MaxCommentLength; Index++)
  490.          strcat (TeXFormat->DummyComment, " ");
  491.       for (Index = 1; Index < TeXFormat->MaxCommentLines; Index++)
  492.          strcat (TeXFormat->DummyComment, NEWLINE);
  493.  
  494.       if (TeXFormat->FormatArray)
  495.          {
  496.          for (Index = 0, NewNode = TeXFormat->FirstNode; Index < TeXFormat->NoOfFormats; Index++, NewNode = NewNode->NextNode)
  497.             TeXFormat->FormatArray[Index]  = NewNode->Name;
  498.  
  499.          strsrt (TeXFormat->FormatArray, TeXFormat->NoOfFormats);
  500.          TeXFormat->FormatArray[TeXFormat->NoOfFormats]  = NULL;
  501.  
  502.          for (NewNode = TeXFormat->FirstNode; NewNode != NULL; NewNode = NewNode->NextNode)
  503.             for (Index = 0; Index < TeXFormat->NoOfFormats; Index++)
  504.                if (TeXFormat->FormatArray[Index] == NewNode->Name)
  505.                   NewNode->ID = Index;
  506.          }
  507.       else
  508.          return (NO_MEMORY);
  509.       }
  510.    else
  511.       return (NO_LOCK);
  512.    }
  513. else
  514.    return (NO_ENV);
  515.  
  516. return (SCAN_OK);
  517. }
  518.  
  519.  
  520. /***************************************************************************
  521.  *                                                                         *
  522.  *  Function name : HandleIDCMP                                            *
  523.  *                                                                         *
  524.  *  Description : Tests IDCMP port for incoming messages.                  *
  525.  *                This function returns if the window close gaget or the   *
  526.  *                DONE gadget was activated.                               *
  527.  *                                                                         *
  528.  ***************************************************************************
  529.  *                                                                         *
  530.  *  Synopsis : HandleIDCMP (TeXFormat, Settings)                           *
  531.  *                                                                         *
  532.  *  Parameters :                                                           *
  533.  *    (struct TeXFormat *) TeXFormat:                                      *
  534.  *          This structure contains the names of the TeX formats available.*
  535.  *    (struct Settings *) Settings:                                        *
  536.  *          Contains the path and extension of the format files and the    *
  537.  *          name of the TEXFORMAT enviroenment variable.                   *
  538.  *                                                                         *
  539.  *  Return value : None                                                    *
  540.  *                                                                         *
  541.  ***************************************************************************/
  542.  
  543. void HandleIDCMP (struct TeXFormat *TeXFormat, struct Settings *Settings)
  544.  
  545. {
  546. BOOL                 Running = TRUE;
  547. ULONG                Signal;
  548. struct FormatNode   *FNode;
  549. int                  FormatID;
  550. char                 CMDStrg[128];
  551.  
  552.  
  553. while (Running)
  554.    {
  555.    switch (DoMethod (App, MUIM_Application_Input, &Signal))
  556.       {
  557.       case MUIV_Application_ReturnID_Quit:
  558.          Running = FALSE;
  559.          break;
  560.  
  561.       case ID_HIDE:
  562.          set (Description_TextField, MUIA_Text_Contents, TeXFormat->DummyComment);
  563.          break;
  564.  
  565.       case ID_SHOW:
  566.          set (Description_TextField, MUIA_Text_Contents, TeXFormat->CurrentFormat->Comment);
  567.          break;
  568.  
  569.       case ID_FORMAT_SEL:
  570.          get (Select_Gad, MUIA_Radio_Active, &FormatID);
  571.          for (FNode = TeXFormat->FirstNode; FNode != NULL; FNode = FNode->NextNode)
  572.             if (FNode->ID == FormatID) break;
  573.          TeXFormat->CurrentFormat = FNode;
  574.          set (Description_TextField, MUIA_Text_Contents, FNode->Comment);
  575.          SetVar (Settings->ArgStr[KW_CURRENTFORMAT], FNode->Name, strlen (FNode->Name), GVF_GLOBAL_ONLY);
  576.          break;
  577.  
  578.       case ID_SAVE:
  579.          sprintf (CMDStrg, "copy env:%s envarc:%s", Settings->ArgStr[KW_CURRENTFORMAT], Settings->ArgStr[KW_CURRENTFORMAT]);
  580.          system (CMDStrg);
  581.          break;
  582.  
  583.       case ID_ABOUT:
  584.            MUI_Request (App, Win, 0, NULL, "OK", "\n               TeXFormat %s         \n\n     A Format File Selector for TeX \n\n        (c) 1993 by Richard Bödi\n\n Mathematisches Institut\n Universität Tübingen\n Auf der Morgenstelle 10\n 72076 Tübingen\n Germany\n\ne-mail :\n mmisa01@mailserv.zdv.uni-tuebingen.de\n");
  585.          break;
  586.       }
  587.  
  588.     if (Running && Signal) Wait (Signal);
  589.    }
  590. }
  591.  
  592.  
  593. /***************************************************************************
  594.  *                                                                         *
  595.  *  Function name : LineLength                                             *
  596.  *                                                                         *
  597.  *  Description : Determines the length of the longest lines of a string   *
  598.  *                which contains '\n' as a newline token.                  *
  599.  *                                                                         *
  600.  ***************************************************************************
  601.  *                                                                         *
  602.  *  Synopsis : LineLength (Strg)                                           *
  603.  *                                                                         *
  604.  *  Parameters :                                                           *
  605.  *    (char *) Strg:                                                       *
  606.  *          The input string.                                              *
  607.  *                                                                         *
  608.  *  Return value :                                                         *
  609.  *    (int) The length of the longest line.                                *
  610.  *                                                                         *
  611.  ***************************************************************************/
  612.  
  613. int   LineLength (char *Strg)
  614.  
  615. {
  616. char  Buffer[COMMENTLENGTH];
  617. char *String = Strg;
  618. int   MaxLength = 0;
  619.  
  620. while (1)
  621.    {
  622.    String = stptok (String, Buffer, sizeof (Buffer), "\\");
  623.    if (strlen (Buffer) > MaxLength) MaxLength = strlen (Buffer);
  624.    if (String[0] == '\0') break;
  625.    if (String[1] == 'n')  String++;
  626.    String++;
  627.    }
  628.  
  629. return (MaxLength);
  630. }
  631.  
  632.  
  633. /***************************************************************************
  634.  *                                                                         *
  635.  *  Function name : NoOfLines                                              *
  636.  *                                                                         *
  637.  *  Description : Determines the number of lines of a string               *
  638.  *                which contains '\n' as a newline token.                  *
  639.  *                                                                         *
  640.  ***************************************************************************
  641.  *                                                                         *
  642.  *  Synopsis : NoOfLines (Strg)                                            *
  643.  *                                                                         *
  644.  *  Parameters :                                                           *
  645.  *    (char *) Strg:                                                       *
  646.  *          The input string.                                              *
  647.  *                                                                         *
  648.  *  Return value :                                                         *
  649.  *    (int) The number of lines in the string.                             *
  650.  *                                                                         *
  651.  ***************************************************************************/
  652.  
  653. int   NoOfLines (char *Strg)
  654.  
  655. {
  656. char  Buffer[COMMENTLENGTH];
  657. char *String = Strg;
  658. int   Lines = 1;
  659.  
  660. while (1)
  661.    {
  662.    String = stptok (String, Buffer, sizeof (Buffer), "\\");
  663.    if (String[0] == '\0') break;
  664.    if (String[1] == 'n')
  665.       {
  666.       String++;
  667.       Lines++;
  668.       }
  669.    String++;
  670.    }
  671.  
  672. return (Lines);
  673. }
  674.  
  675.  
  676. /***************************************************************************
  677.  *                                                                         *
  678.  *  Function name : ConvertNL                                              *
  679.  *                                                                         *
  680.  *  Description : Converts \n to newline characters.                       *
  681.  *                                                                         *
  682.  ***************************************************************************
  683.  *                                                                         *
  684.  *  Synopsis : ConvertNL (Strg)                                            *
  685.  *                                                                         *
  686.  *  Parameters :                                                           *
  687.  *    (char *) Strg:                                                       *
  688.  *          The input string.                                              *
  689.  *                                                                         *
  690.  *  Return value :                                                         *
  691.  *    (char *) Same as the input string.                                   *
  692.  *                                                                         *
  693.  ***************************************************************************/
  694.  
  695. char  *ConvertNL (char *Strg)
  696.  
  697. {
  698. char  Buffer[COMMENTLENGTH];
  699. char *String = Strg;
  700.  
  701. while (1)
  702.    {
  703.    String = stptok (String, Buffer, sizeof (Buffer), "\\");
  704.    if (String[0] == '\0') break;
  705.    if (String[1] == 'n')
  706.       {
  707.       String[0] = ' '; String[1] = '\n';
  708.       String++;
  709.       }
  710.    String++;
  711.    }
  712.  
  713. return (Strg);
  714. }
  715.  
  716.  
  717.  
  718. /***************************************************************************
  719.  *                                                                         *
  720.  *  Function name : OpenLibs                                               *
  721.  *                                                                         *
  722.  *  Description : Opens the following libraries:                           *
  723.  *                 mui.library                                             *
  724.  *                                                                         *
  725.  ***************************************************************************
  726.  *                                                                         *
  727.  *  Synopsis : OpenLibs ()                                                 *
  728.  *                                                                         *
  729.  *  Parameters : None                                                      *
  730.  *                                                                         *
  731.  *  Return value : BOOL                                                    *
  732.  *    TRUE, if successful                                                  *
  733.  *   FALSE, if not.                                                        *
  734.  *                                                                         *
  735.  ***************************************************************************/
  736.  
  737. BOOL OpenLibs ()
  738.  
  739. {
  740. if (MUIMasterBase = (struct Library *)OpenLibrary (MUIMASTER_NAME, MUIMASTER_VMIN))
  741.    return (TRUE);
  742. else
  743.    return (FALSE);
  744. }
  745.  
  746.  
  747. /***************************************************************************
  748.  *                                                                         *
  749.  *  Function name : Cleanup                                                *
  750.  *                                                                         *
  751.  *  Description : Frees all allocated memory, closes down the MUI          *
  752.  *                application, possibly prints some error message and      *
  753.  *                exits.                                                   *
  754.  *                                                                         *
  755.  ***************************************************************************
  756.  *                                                                         *
  757.  *  Synopsis : Cleanup (Strg, ErrorID)                                     *
  758.  *                                                                         *
  759.  *  Parameters :                                                           *
  760.  *    (char *) Strg:                                                       *
  761.  *       Pointer to a message string.                                      *
  762.  *    (enum ErrorIDs) ErrorID:                                             *
  763.  *       Error code identifier.                                            *
  764.  *                                                                         *
  765.  *  Return value : None                                                    *
  766.  *                                                                         *
  767.  ***************************************************************************/
  768.  
  769. void Cleanup (char *Strg, enum ErrorIDs ErrorID)
  770.  
  771. {
  772. if (Strg)            puts (Strg);
  773. if (App)             MUI_DisposeObject (App);
  774. if (MUIMasterBase)   CloseLibrary (MUIMasterBase);
  775.  
  776. exit (ErrorID);
  777. }
  778.  
  779.